import "dart:math" as math;

import 'package:flutter/material.dart';

class JRLoadingWidget extends StatefulWidget {
  const JRLoadingWidget({super.key});

  @override
  _JRLoadingWidgetState createState() => _JRLoadingWidgetState();
}

class _JRLoadingWidgetState extends State<JRLoadingWidget>
    with SingleTickerProviderStateMixin {
  AnimationController? _controller;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 100 * 15),
    )..repeat();
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  Widget _loadingBtn() {
    Widget icon = Image.asset(
      "assets/images/common/common_loading.png",
      width: 18,
      height: 18,
    );

    if (_controller != null) {
      icon = AnimatedBuilder(
        animation: _controller!,
        builder: (_, Widget? child) {
          return Transform.rotate(
            angle: _controller!.value * 2 * math.pi,
            child: child,
          );
        },
        child: icon,
      );
    }

    return icon;
  }

  @override
  Widget build(BuildContext context) {
    return _loadingBtn();
  }
}
